package com.suretech.nms.subagent.common;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

import org.snmp4j.smi.Variable;

/**
 * @Author: Jacky Fang
 * @Company: Sure Tech (HK) LTD.
 * @Date: 2008-7-25 下午04:17:27
 */

public class TxtFileUtil {
	public  BufferedReader bufread;
	private  String path;
	private  File file = new File(path);
	private  String readStr = "";
	
	public TxtFileUtil(String path){
		this.path=path;
	}
	/**
	 * Create txt file.
	 * 
	 * @throws IOException
	 * 
	 */
	public void creatTxtFile() throws IOException {
		if (!file.exists()) {
			file.createNewFile();
			System.err.println(file + "had created！");
		}
	}

	/**
	 * Read txt file.
	 * 
	 */
	public String readTxtFile() {
		String read;
		FileReader fileread;
		try {
			fileread = new FileReader(file);
			bufread = new BufferedReader(fileread);
			try {
				while ((read = bufread.readLine()) != null) {
					readStr = readStr + read + "\r\n";
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return readStr;
	}

	/** */
	/**
	 * Write txt file.
	 * 
	 */
	public void writeTxtFile(String newStr) throws IOException {
		// Frist read old content, then write
		String filein = newStr + "\r\n" + readStr + "\r\n";
		RandomAccessFile mm = null;
		try {
			mm = new RandomAccessFile(file, "rw");
			mm.writeBytes(filein);
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			if (mm != null) {
				try {
					mm.close();
				} catch (IOException e2) {
					e2.printStackTrace();
				}
			}
		}
	}

	/** */
	/**
	 * Repalce content with new string.
	 * 
	 * @param oldStr
	 *            old string
	 * @param replaceStr
	 *            new string
	 */
	//setColumnValue(Variable index, int col, Variable value)
	public void replaceTxtByStr(Variable index, int col, String replaceStr) {
		String temp = "";
		String newStr= "";
		try {
			File file = new File(path);
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);
			StringBuffer buf = new StringBuffer();

			// Save content before the line
			for (int j = 1; (temp = br.readLine()) != null; j++) {
				String[] strArr=temp.split("[ \t]");
				if (strArr.length>0) {
					if (index.toString().equals(strArr[0])) {
						strArr[col]=replaceStr;
						for (int i = 0; i < strArr.length; i++) {
							newStr+=strArr[i]+" ";
						}
						break;
					}
				}
				buf = buf.append(temp.trim());
				buf = buf.append(System.getProperty("line.separator"));
			}

			// insert content
			buf = buf.append(newStr.trim());

			// save content after the line
			while ((temp = br.readLine()) != null) {
				buf = buf.append(System.getProperty("line.separator"));
				buf = buf.append(temp);
			}

			br.close();
			FileOutputStream fos = new FileOutputStream(file);
			PrintWriter pw = new PrintWriter(fos);
			pw.write(buf.toString().toCharArray());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void main(String[] s) throws IOException {
//		ReadWriteFile.creatTxtFile();
//		ReadWriteFile.readTxtFile();
//		ReadWriteFile.writeTxtFile("20080808:12:13");
//		TxtFileUtil.replaceTxtByStr(new OID("1"),2 ,"aa");
	}

}
